package com.compunit.composite.a;

import java.awt.IllegalComponentStateException;

import org.compunit.interfaces.IComponent;
import org.compunit.interfaces.IResourceRetriever;

import com.compunit.composite.a.interfaces.IPrinter;
import com.compunit.composite.a.interfaces.IStamper;

public abstract class AbstractPrinter implements IPrinter {

	private IStamper stamper;

	@Override
	public void print(final String message) {
		final StringBuilder builder = new StringBuilder();
		builder.append(this.stamper.getStamp()).append(message);
		this.doPrint(builder.toString());
	}
	
	protected abstract void doPrint(final String output);

	@Override
	public boolean activate(final IResourceRetriever handler) throws Exception {
		if(this.stamper == null) throw new IllegalComponentStateException("No available stamper.");
		return true;
	}

	@Override
	public boolean connect(final IComponent unit, final String interfaceName) throws Exception {
		if(unit instanceof IStamper) {
			this.stamper = (IStamper)unit;
			return true;
		}
		return false;
	}
}
